﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Timers;
using System.Configuration;
using System.Net.Mail;
using System.Net;


namespace PMTWindowsService
{
    public partial class PMTWindowsService : ServiceBase
    {
        private Timer scheduleTimer = null;
        private DateTime lastRun;
        private bool flag;

        public PMTWindowsService()
        {
            InitializeComponent();
            if (!System.Diagnostics.EventLog.SourceExists("MySource"))
            {
                System.Diagnostics.EventLog.CreateEventSource(
                    "MySource", "MyNewLog");
            }
            eventLog1.Source = "MySource";
            eventLog1.Log = "MyNewLog";

            scheduleTimer = new Timer();
            scheduleTimer.Interval = 5 * 60 * 1000;//1h after after  mm * sec * milisec
            scheduleTimer.Elapsed += new ElapsedEventHandler(scheduleTimer_Elapsed);
        }

        protected void scheduleTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            try
            {
                LocalWebService.WebService service = new LocalWebService.WebService();
                service.callEveryMin();
            }
            catch (Exception ex)
            {
                sendMail("anamuliut@gmail.com", "Exception in LocalWebService", ex.Message);
            }
        }

        
        private void sendMail(string toMail, string subject, string body)
        {
            
            try
            {
                System.Net.Mail.MailMessage objMailMessage = new System.Net.Mail.MailMessage();
                objMailMessage.From = new MailAddress("anamuliut@gmail.com", "IT-Explorer Server");

                objMailMessage.To.Add(toMail);

                objMailMessage.Subject = subject;

                objMailMessage.Body = body;
                objMailMessage.IsBodyHtml = true;
                System.Net.Mail.SmtpClient objSMTPClient = new System.Net.Mail.SmtpClient("sg2nlvphout-v01.shr.prod.sin2.secureserver.net", 25);

                objSMTPClient.Credentials = CredentialCache.DefaultNetworkCredentials;
                objSMTPClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                objSMTPClient.Send(objMailMessage);
            }
            catch (Exception ex)
            {
            }
        }

        protected override void OnStart(string[] args)
        {
            flag = true;
            lastRun = DateTime.Now;
            scheduleTimer.Start();
            //ServiceEmailMethod();
            eventLog1.WriteEntry("Now Started");
        }

        protected override void OnStop()
        {
            scheduleTimer.Stop();
            eventLog1.WriteEntry("Stopped");
        }

        //protected override void OnContinue()
        //{
        //    eventLog1.WriteEntry("In OnContinue.");
        //}


        protected override void OnPause()
        {
            scheduleTimer.Stop();
            eventLog1.WriteEntry("Paused");
        }
        protected override void OnContinue()
        {
            scheduleTimer.Start(); ;
            eventLog1.WriteEntry("Continuing");
        }
        protected override void OnShutdown()
        {
            scheduleTimer.Stop();
            eventLog1.WriteEntry("ShutDowned");
        }
    }
}
